package controllers

import (
	"WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetGoods(c *gin.Context){
	err, info, total := services.GetAllgoods()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func DelGoods(c *gin.Context){
	id := c.PostForm("goodsId")

	if id == ""{
		app.INFO(c,30001,"参数错误")
		return
	}
	err := services.Delgoods(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func UpdateGoods(c *gin.Context){
	id := c.PostForm("goodsId")
	name:=c.PostForm("goodsName")
	gcid:=c.PostForm("goodsClassId")
	number:= c.PostForm("goodsNumber")
	tempNumber := com.StrTo(number).MustInt64()
	price:= c.PostForm("goodsPrice")
	tempPrice := com.StrTo(price).MustInt64()

	if id == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}

	err := services.Updategoods(models.Goods{Goods_id: id, Goods_name: name, Gc_id: gcid, Goods_number: tempNumber, Goods_price: tempPrice})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func AddGoods(c *gin.Context){
	id := c.PostForm("goodsId")
	name:=c.PostForm("goodsName")
	gcid:=c.PostForm("goodsClassId")
	number:= c.PostForm("goodsNumber")
	tempNumber := com.StrTo(number).MustInt64()
	price:= c.PostForm("goodsPrice")
	tempPrice := com.StrTo(price).MustInt64()
	factoryId := c.PostForm("factoryId")

	err := services.Addgoods(models.Goods{Goods_id: id, Goods_name: name, Gc_id: gcid, Goods_number: tempNumber, Goods_price: tempPrice,Factory_id: factoryId})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}